//
//  GMFShapeView.m
//  SplunkMobile
//
//  Created by lijunying on 1/21/2016.
//  Copyright © 2016 Gome. All rights reserved.
//

#import "GMFShapeView.h"
#import "GMFShapeButton.h"

@interface GMFShapeView()

@property (nonatomic, assign) int cintButtonCount;
@property (nonatomic, weak) UISlider *sldPenWidth;

@property (nonatomic, weak) UIView *viewSeperateLine;

@property (nonatomic, strong) GMFShapeButton *btnCurrent;

@end


@implementation GMFShapeView

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = GMKColor(51, 51, 51);
        self.alpha = 0;
        
        /// 线
        GMFShapeButton *btnLine = [[GMFShapeButton alloc] init];
        [btnLine setShapeType:0 highlighted:NO];
        btnLine.tag = 200;
        [btnLine setTitle:@"直线" forState:UIControlStateNormal];
        [btnLine addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnLine];
        self.btnLine = btnLine;
        _cintButtonCount++;
        
        /// 笔
        GMFShapeButton *btnPen = [[GMFShapeButton alloc] init];
        [btnPen setShapeType:1 highlighted:YES];
        btnPen.tag = 201;
        [btnPen setTitle:@"自由线" forState:UIControlStateNormal];
        [btnPen addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnPen];
        self.btnPen = btnPen;
        _cintButtonCount++;
        
        /// 矩形
        GMFShapeButton *btnRectangle = [[GMFShapeButton alloc] init];
        [btnRectangle setShapeType:2 highlighted:NO];
        btnRectangle.tag = 202;
        [btnRectangle setTitle:@"矩形" forState:UIControlStateNormal];
        [btnRectangle addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnRectangle];
        self.btnRectangle = btnRectangle;
        _cintButtonCount++;
        
        /// 椭圆形
        GMFShapeButton *btnEllipse = [[GMFShapeButton alloc] init];
        [btnEllipse setShapeType:3 highlighted:NO];
        btnEllipse.tag = 203;
        [btnEllipse setTitle:@"椭圆形" forState:UIControlStateNormal];
        [btnEllipse addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnEllipse];
        self.btnEllipse = btnEllipse;
        _cintButtonCount++;
        
//        UIView *viewSeperateLine = [[UIView alloc] init];
//        viewSeperateLine.backgroundColor = GMKColor(210, 210, 210);
//        [self addSubview:viewSeperateLine];
//        self.viewSeperateLine = viewSeperateLine;
        
        _btnCurrent = [[GMFShapeButton alloc] init];
        _btnCurrent = _btnPen;
        
        /// 滑条
//        UISlider *sldPenWidth = [[UISlider alloc] init];
//        sldPenWidth.minimumValue = 1.0;
//        sldPenWidth.maximumValue = 5.0;
//        sldPenWidth.value = 2.0;
//        [sldPenWidth addTarget:self action:@selector(sliderValueChanged) forControlEvents:UIControlEventValueChanged];
//        [self addSubview:sldPenWidth];
//        self.sldPenWidth = sldPenWidth;
    }
    
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat fltPadding = 15;
//    CGFloat fltPaddingBetweenButtons = (screenW * 0.5 - fltPadding * 2 - 35 * _cintButtonCount) / (_cintButtonCount - 1);
    CGFloat fltPaddingBetweenButtons = (screenW - fltPadding * 2 - fltButtonWidth * _cintButtonCount) / (_cintButtonCount - 1);
    
    [_btnLine       setFrame:CGRectMake(fltButtonWidth * 0 + fltPadding, 0, fltButtonWidth, 50)];
    [_btnPen        setFrame:CGRectMake(fltButtonWidth * 1 + fltPadding + fltPaddingBetweenButtons * 1, 0, fltButtonWidth, 50)];
    [_btnRectangle  setFrame:CGRectMake(fltButtonWidth * 2 + fltPadding + fltPaddingBetweenButtons * 2, 0, fltButtonWidth, 50)];
    [_btnEllipse    setFrame:CGRectMake(fltButtonWidth * 3 + fltPadding + fltPaddingBetweenButtons * 3, 0, fltButtonWidth, 50)];
    
//    [_viewSeperateLine   setFrame:CGRectMake(0, 0, screenW, 1)];
    
//    [_sldPenWidth   setFrame:CGRectMake(screenW * 0.6 - fltPadding, 0, screenW * 0.4, fltButtonWidth)];
}


- (void)buttonClick:(GMFShapeButton *)button
{
    if (_btnCurrent.tag != button.tag)
    {
        [_btnCurrent setShapeType:_btnCurrent.tag - 200 highlighted:NO];
        
//        [_btnCurrent selectOrUnselectTheButton:NO];
        _btnCurrent = button;
        [_btnCurrent setShapeType:_btnCurrent.tag - 200 highlighted:YES];
    }
    
    NSString *strButtonTag = [NSString stringWithFormat:@"%ld", (long)button.tag];
    
    if ([self.delegate respondsToSelector:@selector(shapeView:buttonTagString:)])
    {
        [self.delegate shapeView:self buttonTagString:strButtonTag];
    }
}


- (void)sliderValueChanged
{
    int cintLabPenWidthValue = roundf(_sldPenWidth.value);
    
    _sldPenWidth.value = cintLabPenWidthValue;
    
    if ([self.delegate respondsToSelector:@selector(shapeView:sliderValue:)])
    {
        [self.delegate shapeView:self sliderValue:_sldPenWidth.value];
    }
}


@end











